package scala.day03

import scala.collection.immutable
import scala.io.Source
import scala.io.Source._

object Demo06StudentTest {
  def main(args: Array[String]): Unit = {
    /**
      * 3.统计每科都及格的学生
     */
    //计算每个科目都及格的分数线
    //读数据
    val cources: List[String] = fromFile("data/cource.txt").getLines().toList
    //取数据
    val cidAndJgx: Map[String, Double] = cources.map(f =>{
      val split: Array[String] = f.split(",")
      val cid: String = split(0)
      val jgx: Double = split(2).toDouble*0.6
      (cid,jgx)
    }).toMap//可以通过科目id获取及格成绩
    //读数据
    val scores: List[String] = fromFile("data/score.txt").getLines().toList
   //将不及格的科目过滤掉
    val filterScore: List[String] = scores.filter(p => {
      val split: Array[String] = p.split(",")
      //获取科目编号
      val cid: String = split(1)
      //获取科目名称
      val score: Double = split(2).toDouble
      val jgx: Double = cidAndJgx.getOrElse(cid,0.0)
      score >= jgx
    })

    /**
      * 统计学生及格科目数
      */
    //取出学生的id
    val ids: List[String] = filterScore.map(f => f.split(",")(0))
    //根据id分组
    val idGroup: Map[String, List[String]] = ids.groupBy(id => id)
    //统计及格的科目数
    val mapCount: Map[String, Int] = idGroup.map(f => {
      val id: String = f._1
      //及格的科目数
      val length: Int = f._2.length
      (id,length)
    })
    //取出都及格的学生
    val map: Map[String, Int] = mapCount.filter(f => f._2==6)
    //读取students，并取出学号和姓名
    val students: List[String] = fromFile("data/students.txt").getLines().toList
    val idAndName: List[(String, String)] = students.map(f =>{
      val split: Array[String] = f.split(",")
      (split(0),split(1))
    })
    //将学生id换成姓名
   val names: immutable.Iterable[Any] =map.map(f =>{
     val id: String = f._1
     val name: Any = idAndName.toMap.getOrElse(id,0)
     name
   })
    names.foreach(println)
  }
}
